#include "openglwindiow.h"

OpenGLWindiow::OpenGLWindiow(QOpenGLWindow *parent)
    : QOpenGLWindow(QOpenGLWindow::NoPartialUpdate, parent), context(nullptr)
{
    this->setSurfaceType(QWindow::OpenGLSurface);
//    initializeGL();
//    paintGL();
}

OpenGLWindiow::~OpenGLWindiow()
{

}

void OpenGLWindiow::initializeGL()
{
    if(!context) {
        context = new QOpenGLContext(this);
        context->create();
    }
    context->makeCurrent(this);
    initializeOpenGLFunctions();
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    debugMessage();
}

void OpenGLWindiow::resizeGL(int w, int h)
{
    Q_UNUSED(w)
    Q_UNUSED(h)
}

void OpenGLWindiow::paintGL()
{
    GLfloat a = 1 / (2 - 2*cos(72*M_PI/180));
    GLfloat bx = a * cos(18*M_PI/180);
    GLfloat by = a * sin(18*M_PI/180);
    GLfloat cy = -a * cos(18*M_PI/180);
    GLfloat PointA[2] = {0, a};
    GLfloat PointB[2] = {bx, by};
    GLfloat PointC[2] = {0.5, cy};
    GLfloat PointD[2] = {-0.5, cy};
    GLfloat PointE[2] = {-bx, by};

    glBegin(GL_LINE_LOOP);
    glVertex2fv(PointA);
    glVertex2fv(PointC);
    glVertex2fv(PointE);
    glVertex2fv(PointB);
    glVertex2fv(PointD);
    glEnd();
    debugVersion();
}

void OpenGLWindiow::debugMessage()
{
    qDebug() << this->format().version();
    qDebug() << QOpenGLContext::currentContext()->openGLModuleType();
    qDebug() << QOpenGLContext::currentContext()->isOpenGLES();
}

void OpenGLWindiow::debugVersion()
{
    const GLubyte *getByte = glGetString(GL_VERSION);
    const char *temp = (const char *)getByte;
    qDebug() << "OpenGL Version:" << temp;
    getByte = glGetString(GL_VENDOR);
    temp = (const char *)getByte;
    qDebug() << "OpenGL Vendor:" << temp;
    getByte = glGetString(GL_RENDERER);
    temp = (const char *)getByte;
    qDebug() << "OpenGL Renderer:" << temp;
    getByte = glGetString(GL_SHADING_LANGUAGE_VERSION);
    temp = (const char *)getByte;
    qDebug() << "OpenGL Shading Language Version:" << temp;
}
